
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>插件 · Gitbook demo</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        <meta name="author" content="lanyue">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="strict.html" />
    
    
    <link rel="prev" href="structure.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/tree/1.0/docs">
            
                    
                    寻找 1.0 文档？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/releases">
            
                    
                    更新记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="installation.html">
            
                <a href="installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    Vuex 是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    核心概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="state.html">
            
                <a href="state.html">
            
                    
                    State
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="getters.html">
            
                <a href="getters.html">
            
                    
                    Getters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="mutations.html">
            
                <a href="mutations.html">
            
                    
                    Mutations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    Modules
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="structure.html">
            
                <a href="structure.html">
            
                    
                    项目结构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.9" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="strict.html">
            
                <a href="strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="forms.html">
            
                <a href="forms.html">
            
                    
                    表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="hot-reload.html">
            
                <a href="hot-reload.html">
            
                    
                    热重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API 文档
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >插件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x63D2;&#x4EF6;">&#x63D2;&#x4EF6;</h1>
<p>Vuex &#x7684; store &#x63A5;&#x53D7; <code>plugins</code> &#x9009;&#x9879;&#xFF0C;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#x66B4;&#x9732;&#x51FA;&#x6BCF;&#x6B21; mutation &#x7684;&#x94A9;&#x5B50;&#x3002;Vuex &#x63D2;&#x4EF6;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x63A5;&#x6536; store &#x4F5C;&#x4E3A;&#x552F;&#x4E00;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> myPlugin = store =&gt; {
  <span class="hljs-comment">// &#x5F53; store &#x521D;&#x59CB;&#x5316;&#x540E;&#x8C03;&#x7528;</span>
  store.subscribe((mutation, state) =&gt; {
    <span class="hljs-comment">// &#x6BCF;&#x6B21; mutation &#x4E4B;&#x540E;&#x8C03;&#x7528;</span>
    <span class="hljs-comment">// mutation &#x7684;&#x683C;&#x5F0F;&#x4E3A; { type, payload }</span>
  })
}
</code></pre>
<p>&#x7136;&#x540E;&#x50CF;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  <span class="hljs-comment">// ...</span>
  plugins: [myPlugin]
})
</code></pre>
<h3 id="&#x5728;&#x63D2;&#x4EF6;&#x5185;&#x63D0;&#x4EA4;-mutation">&#x5728;&#x63D2;&#x4EF6;&#x5185;&#x63D0;&#x4EA4; Mutation</h3>
<p>&#x5728;&#x63D2;&#x4EF6;&#x4E2D;&#x4E0D;&#x5141;&#x8BB8;&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x72B6;&#x6001;&#x2014;&#x2014;&#x7C7B;&#x4F3C;&#x4E8E;&#x7EC4;&#x4EF6;&#xFF0C;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x63D0;&#x4EA4; mutation &#x6765;&#x89E6;&#x53D1;&#x53D8;&#x5316;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x63D0;&#x4EA4; mutation&#xFF0C;&#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x540C;&#x6B65;&#x6570;&#x636E;&#x6E90;&#x5230; store&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x540C;&#x6B65; websocket &#x6570;&#x636E;&#x6E90;&#x5230; store&#xFF08;&#x4E0B;&#x9762;&#x662F;&#x4E2A;&#x5927;&#x6982;&#x4F8B;&#x5B50;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A; <code>createPlugin</code> &#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x6709;&#x66F4;&#x591A;&#x9009;&#x9879;&#x6765;&#x5B8C;&#x6210;&#x590D;&#x6742;&#x4EFB;&#x52A1;&#xFF09;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createWebSocketPlugin</span> (<span class="hljs-params">socket</span>) </span>{
  <span class="hljs-keyword">return</span> store =&gt; {
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, data =&gt; {
      store.commit(<span class="hljs-string">&apos;receiveData&apos;</span>, data)
    })
    store.subscribe(mutation =&gt; {
      <span class="hljs-keyword">if</span> (mutation.type === <span class="hljs-string">&apos;UPDATE_DATA&apos;</span>) {
        socket.emit(<span class="hljs-string">&apos;update&apos;</span>, mutation.payload)
      }
    })
  }
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> plugin = createWebSocketPlugin(socket)

<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  state,
  mutations,
  plugins: [plugin]
})
</code></pre>
<h3 id="&#x751F;&#x6210;-state-&#x5FEB;&#x7167;">&#x751F;&#x6210; State &#x5FEB;&#x7167;</h3>
<p>&#x6709;&#x65F6;&#x5019;&#x63D2;&#x4EF6;&#x9700;&#x8981;&#x83B7;&#x5F97;&#x72B6;&#x6001;&#x7684;&#x300E;&#x5FEB;&#x7167;&#x300F;&#xFF0C;&#x6BD4;&#x8F83;&#x6539;&#x53D8;&#x7684;&#x524D;&#x540E;&#x72B6;&#x6001;&#x3002;&#x60F3;&#x8981;&#x5B9E;&#x73B0;&#x8FD9;&#x9879;&#x529F;&#x80FD;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x5BF9;&#x72B6;&#x6001;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x6DF1;&#x62F7;&#x8D1D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> myPluginWithSnapshot = store =&gt; {
  <span class="hljs-keyword">let</span> prevState = _.cloneDeep(store.state)
  store.subscribe((mutation, state) =&gt; {
    <span class="hljs-keyword">let</span> nextState = _.cloneDeep(state)

    <span class="hljs-comment">// &#x6BD4;&#x8F83; prevState &#x548C; nextState...</span>

    <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x72B6;&#x6001;&#xFF0C;&#x7528;&#x4E8E;&#x4E0B;&#x4E00;&#x6B21; mutation</span>
    prevState = nextState
  })
}
</code></pre>
<p><strong>&#x751F;&#x6210;&#x72B6;&#x6001;&#x5FEB;&#x7167;&#x7684;&#x63D2;&#x4EF6;&#x5E94;&#x8BE5;&#x53EA;&#x5728;&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#x4F7F;&#x7528;</strong>&#xFF0C;&#x4F7F;&#x7528; Webpack &#x6216; Browserify&#xFF0C;&#x8BA9;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x5E2E;&#x6211;&#x4EEC;&#x5904;&#x7406;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  <span class="hljs-comment">// ...</span>
  plugins: process.env.NODE_ENV !== <span class="hljs-string">&apos;production&apos;</span>
    ? [myPluginWithSnapshot]
    : []
})
</code></pre>
<p>&#x4E0A;&#x9762;&#x63D2;&#x4EF6;&#x4F1A;&#x9ED8;&#x8BA4;&#x542F;&#x7528;&#x3002;&#x5728;&#x53D1;&#x5E03;&#x9636;&#x6BB5;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x4F7F;&#x7528; Webpack &#x7684; <a href="https://webpack.github.io/docs/list-of-plugins.html#defineplugin" target="_blank">DefinePlugin</a> &#x6216;&#x8005;&#x662F; Browserify &#x7684; <a href="https://github.com/hughsk/envify" target="_blank">envify</a> &#x4F7F; <code>process.env.NODE_ENV !== &apos;production&apos;</code> &#x4E3A; <code>false</code>&#x3002;</p>
<h3 id="&#x5185;&#x7F6E;-logger-&#x63D2;&#x4EF6;">&#x5185;&#x7F6E; Logger &#x63D2;&#x4EF6;</h3>
<blockquote>
<p>&#x5982;&#x679C;&#x6B63;&#x5728;&#x4F7F;&#x7528; <a href="https://github.com/vuejs/vue-devtools" target="_blank">vue-devtools</a>&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4E0D;&#x9700;&#x8981;&#x6B64;&#x63D2;&#x4EF6;&#x3002;</p>
</blockquote>
<p>Vuex &#x81EA;&#x5E26;&#x4E00;&#x4E2A;&#x65E5;&#x5FD7;&#x63D2;&#x4EF6;&#x7528;&#x4E8E;&#x4E00;&#x822C;&#x7684;&#x8C03;&#x8BD5;:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> createLogger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vuex/dist/logger&apos;</span>

<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  plugins: [createLogger()]
})
</code></pre>
<p><code>createLogger</code> &#x51FD;&#x6570;&#x6709;&#x51E0;&#x4E2A;&#x914D;&#x7F6E;&#x9879;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> logger = createLogger({
  collapsed: <span class="hljs-literal">false</span>, <span class="hljs-comment">// &#x81EA;&#x52A8;&#x5C55;&#x5F00;&#x8BB0;&#x5F55;&#x7684; mutation</span>
  transformer (state) {
    <span class="hljs-comment">// &#x5728;&#x5F00;&#x59CB;&#x8BB0;&#x5F55;&#x4E4B;&#x524D;&#x8F6C;&#x6362;&#x72B6;&#x6001;</span>
    <span class="hljs-comment">// &#x4F8B;&#x5982;&#xFF0C;&#x53EA;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x7684;&#x5B50;&#x6811;</span>
    <span class="hljs-keyword">return</span> state.subTree
  },
  mutationTransformer (mutation) {
    <span class="hljs-comment">// mutation &#x6309;&#x7167; { type, payload } &#x683C;&#x5F0F;&#x8BB0;&#x5F55;</span>
    <span class="hljs-comment">// &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6309;&#x4EFB;&#x610F;&#x65B9;&#x5F0F;&#x683C;&#x5F0F;&#x5316;</span>
    <span class="hljs-keyword">return</span> mutation.type
  }
})
</code></pre>
<p>&#x65E5;&#x5FD7;&#x63D2;&#x4EF6;&#x8FD8;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7; <code>&lt;script&gt;</code> &#x6807;&#x7B7E;&#x5F15;&#x5165;&#xFF0C;&#x5B83;&#x4F1A;&#x63D0;&#x4F9B;&#x5168;&#x5C40;&#x65B9;&#x6CD5; <code>createVuexLogger</code>&#x3002;</p>
<p>&#x8981;&#x6CE8;&#x610F;&#xFF0C;logger &#x63D2;&#x4EF6;&#x4F1A;&#x751F;&#x6210;&#x72B6;&#x6001;&#x5FEB;&#x7167;&#xFF0C;&#x6240;&#x4EE5;&#x4EC5;&#x5728;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4F7F;&#x7528;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="structure.html" class="navigation navigation-prev " aria-label="Previous page: 项目结构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="strict.html" class="navigation navigation-next " aria-label="Next page: 严格模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"插件","level":"1.9","depth":1,"next":{"title":"严格模式","level":"1.10","depth":1,"path":"strict.md","ref":"strict.md","articles":[]},"previous":{"title":"项目结构","level":"1.8","depth":1,"path":"structure.md","ref":"structure.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lanyue","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gitbook demo","language":"zh-hans","gitbook":">=3.0.0","description":"this is a test"},"file":{"path":"plugins.md","mtime":"2017-02-21T09:41:56.411Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T15:58:45.473Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

